package bg.btanev.eightyfour.domain.profile.feed;

import java.util.Collection;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.Tag;
import bg.btanev.eightyfour.domain.Target;
import bg.btanev.eightyfour.domain.profile.Profile;
import bg.btanev.eightyfour.domain.profile.ProfileEventsUpdateStrategy;
import bg.btanev.eightyfour.shared.state.ProfileType;

/**
 *
 * @author B.Tanev
 *
 */
public class FeedProfile extends Profile<FeedProfileEvent> {
	/**
	 *
	 */
	private static final long serialVersionUID = 7780554922948613438L;

	private String feedUrl;

	protected FeedProfile() {
		super();
	}

	public FeedProfile(String name, ProfileType type, Target target, String feedUrl) {
		this((Long) null, name, type, target, feedUrl, null, null);
	}

	public FeedProfile(Profile<FeedProfileEvent> profile, String feedUrl) {
		this(profile.getId(), profile.getName(), profile.getType(), profile.getTarget(), feedUrl, profile.getTags(),
				profile.getProfileEvents());
	}

	public FeedProfile(String name, ProfileType type, Target target, String feedUrl, Collection<Tag> tags,
			Collection<FeedProfileEvent> profileEvents) {
		this(null, name, type, target, feedUrl, tags, profileEvents);
	}

	public FeedProfile(Long id, String name, ProfileType type, Target target, String feedUrl, Collection<Tag> tags,
			Collection<FeedProfileEvent> profileEvents) {

		super(id, name, type, target);

		setFeedUrl(feedUrl);

		if (profileEvents != null) {
			this.addProfileEvents(profileEvents);
		}
		if (tags != null) {
			this.addTags(tags);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected <S extends ProfileEventsUpdateStrategy<? extends Profile<FeedProfileEvent>, FeedProfileEvent>> S createProfileEventsUpdateStrategy() {
		return (S) FeedProfileEventUpdateStrategy.getInstance();
	}

	public String getFeedUrl() {
		return feedUrl;
	}

	public void setFeedUrl(String feedUrl) {
		Validate.notEmpty(feedUrl, "Feed url cannot be empty.");
		this.feedUrl = feedUrl;
	}

	@Override
	public String toString() {
		return "FeedProfile [feedUrl=" + feedUrl + ", toString()=" + super.toString() + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((feedUrl == null) ? 0 : feedUrl.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		FeedProfile other = (FeedProfile) obj;
		if (feedUrl == null) {
			if (other.feedUrl != null)
				return false;
		} else if (!feedUrl.equals(other.feedUrl))
			return false;
		return true;
	}
}
